const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    shop: {
      type: Object,
      value: {}
    },
    price: {
      type: Number,
    },
    urls: {
      type: String
    },
    totalprice: {
      type: Number
    },
    restaurantId: {
      type: String
    },
    billId: {
      type: Number
    },
    order: {
      type: String
    },
    people: {
      type: Number
    },
    textDis: {
      type: Boolean
    },
    types: {
      type: Number
    },
    addressid: {
      type: String,
    },
    logfee:{
      type:Number
    },
    address:{
      type:Object
    },
    total:{
      type:Number
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    diningList: [],
    orderTitle: '', //支付标题
    cantingid: '', //餐厅id
    select: 1,
    wxImg: ['../../images/wx.png', '../../images/wx-select.png'], //微信支付图片路径
    bankImg: ['../../images/bank.png', '../../images/bank-select.png'], //会员卡支付图片路径
    youhui: '../../images/youhui.png',
    togglePopup: false,
    luckcard: [],
    totalprice: '',
    billId: '', //账单id
    order: '', //dingdanid
    people: 0,
    clickTimes:'true',
    id: '', //优惠id
    voucher: '', //几折劵
    textDis: true,
    addressid: '',
    address:"",
    types: '',
    payorder: '支付订单',
    disabled: false,
    quxiao:'',
    vercherPrice:'',//折后价
    hidden:true,//使用打折券提示
    hidden1:true,//实际价格提示
    hidden2:true,//取消按钮
    logfee:'',//物流费
    textareaInput:"",
    total:''
  },
  
  ready() {

    this.setData({
      logfee: this.properties.logfee,
      cantingid: this.properties.restaurantId,
      billId: this.properties.billId,
      totalprice: this.properties.totalprice,
      vercherPrice: this.properties.totalprice,
      realMoney: this.properties.totalprice,
      order: this.properties.order,
      people: this.properties.people,
      addressid: this.properties.addressid,
      types: this.properties.types,
      address: this.properties.address,
      total:this.properties.total
    })
    console.log('vercherPrice',this.properties.vercherPrice)
    console.log(this.properties.types)
    app.gets(app.url.getRestaurantInfo + '?restaurantId=' + this.data.cantingid).then(res => {
      var title =res.data.data[0].NAME
      if(title==""||title==null){
        this.setData({
          diningList: res.data.data,
          orderTitle: '这是默认的标题'
        })
      }
      this.setData({
        diningList: res.data.data,
        orderTitle: res.data.data[0].NAME
      })

    })

     
    
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //点击倒计时
    clickTime() {
      var that = this
      setTimeout(() => {
        this.setData({
          clickTimes: 'true'
        });
        console.log('点击倒计时', that.data.clickTimes)
      }, 3000);

    },
    radioChange(e) {
      this.setData({
        select: e.detail.value
      })
      console.log(this.data.select)
    },
    // 子组件向父组件传值
    getMemberLuck() {
      var userId = app.globalDataO.openid.objectSid
      var cantingid = this.data.cantingid
      app.post(app.url.getMemberLuck, {
        memberId: userId,
        restaurant_id: cantingid
      }).then(res => {
        console.log(res.data.data)
        this.setData({
          luckcard: res.data.data
        })
      })
    },
    //取消订单
    orderpass(){
       var that=this
      var logfee = that.data.logfee
      let order = that.data.order
      var consumeMoney = that.data.totalprice
      var realMoney = that.data.total
      app.post(app.url.order.orderpass, { billCode: order, realMoney: realMoney, consumeMoney: consumeMoney, luckId:that.data.id}).then(res=>{
        let ordetType = {
          status: 6,
          name: 'remove',
        }
        wx.setStorageSync('ordetType', JSON.stringify(ordetType))
        that.setData({
          disabled: true,
          payorder: '未支付'
        })
       if(res.data.code==0){
         wx.switchTab({
           url: '../../pages/orderlist/orderlist',
         })
       }
      })
    },
    //取消使用优惠券
    quxiao(){
      let total=this.data.totalprice
      this.setData({
        total:total,
        hidden:true,
        hidden1:true,
        hidden2:true,
        voucher: '',
      })
    },
    //备注
    textarea_input(e){
this.setData({
  textareaInput:e.detail.value
})
    },
  // 使用优惠券
    user_(e) {
      var billId = this.data.billId
      var cantingid = this.data.cantingid
      var id = e.currentTarget.dataset.id
      var vercher = e.currentTarget.dataset.vercher
      var logfee = this.data.logfee
      var consumeMoney = this.data.totalprice
      this.setData({

      })
      console.log(id + 'can' + billId + 'bill' + cantingid)
      app.post(app.url.updateprice, {
        restaurant_id: cantingid,
        bill_id: billId,
        luck_id: id,
        customerMoney: consumeMoney
      }).then(res => {
        var totalprice = res.data.totalprice + res.data.price
        totalprice = totalprice.toFixed(2)
        if(totalprice<=0){
          totalprice=0.1
        }
        this.setData({
          total: totalprice,
          id: id,
          voucher: vercher + '元',
          quxiao:'取消',
          hidden:false,
          hidden1:false,
          hidden2:false
        })
        this.setData({
          showPopup: false,
          textDis: true
        });
        this.getMemberLuck()
      })
    },
    togglePopup() {
      if (this.data.types == 4) {
        if (this.data.address == "" || this.data.address == null) {
          wx.showToast({
            title: '请选择收货地址',
            image: '../../images/gantan.png'
          })
          return false;
        }
      }
      this.getMemberLuck()
      this.setData({
        showPopup: !this.data.showPopup,
        textDis: !this.data.textDis
      });
    },
    //会员卡支付
    selectPAY() {
      if (this.data.select == 0) {
        this.vipPay()
      }
      if (this.data.select == 1) {
        this.isPay()
      }
    },
    //防连点支付
    isPay(){
      if(this.data.clickTimes=='true'){
        this.data.clickTimes='false'
        this.clickTime()
        this.wxPay()
      }else{
        console.log(this.data.clickTimes,'倒计时未结束')
      }
    },
    //微信支付
    wxPay() {
      let that = this
      if(that.data.types==4){
        console.log(that.data.address)
        if (that.data.address == "" || that.data.address == null) {
          wx.showToast({
            title: '请选择收货地址',
            image: '../../images/gantan.png'
          })
          return false;
        }
      }
      var logfee = that.data.logfee
      let textareaInput = that.data.textareaInput
      let openid = app.globalDataO.openid.openid
      let order = that.data.order
      let orderTitle = that.data.orderTitle
      let totalprice = that.data.totalprice
      var realMoney = that.data.total
      var addressid = that.data.addressid
      var consumeMoney = that.data.totalprice
      var luck_id = that.data.id
      if (realMoney <= 0) {
        realMoney = 0.1
      }
      // console.log(totalprice, '1', realMoney, 'dd', consumeMoney, 'sssssssssssss',addressid)
      app.post(app.url.wxPay, {
        openid: openid,
        payOrderTitle: orderTitle,
        payOrderId: order,
        addressId: addressid,
        realMoney: realMoney,
        logfee: logfee,
        consumeMoney: consumeMoney,
        luck_id: luck_id,
        decsription: textareaInput
      }).then(res => {
        console.log(res)
        if(res.data.success==true){
 
          let nonceStr = res.data.data.nonceStr
          let packages = res.data.data.package
          let paysign = res.data.data.paySign
          let timeStamp = res.data.data.timeStamp
          let signtype = res.data.data.signType
          wx.requestPayment({
            timeStamp: timeStamp,
            nonceStr: nonceStr,
            paySign: paysign,
            signType: signtype,
            package: packages,
            success(res) {
              console.log(res)
              wx.showToast({
                title: '支付成功',
              })
              let ordetType = {
                status: 5,
                name: 'pay',
              }
              wx.setStorageSync('ordetType', JSON.stringify(ordetType))
              wx.switchTab({
                url: '../../pages/orderlist/orderlist',
              })
            },
            fail() {
              wx.showToast({
                image: '../../images/gantan.png',
                title: '支付取消',
              })
              that.setData({
                disabled: true,
                payorder: '已支付'
              })
              that.orderpass()
            }
          })
        }else{
          wx.showToast({
            title: '网络错误',
            image:'../../images/gantan.png'
          })
        }

      })
    },
    //抽奖弹框
    //支付成功回调
  //   wxNotify() {
  //     var order = this.data.order
  //     var totalprice = this.data.totalprice
  //     var logfee = this.data.logfee
  //     var realmoney = this.data.totalprice
  //     app.post(app.url.wxNotify, {
  //       billCode: order,
  //       realMoney: totalprice,
  //       consumeMoney: realmoney,
  //       logfee: logfee
  //     }).then(res => {
  //       console.log(res)
  //       this.setData({
  //         payorder: '支付完成',
  //         disabled: true
  //       })
  //  wx.switchTab({
  //    url: '../../pages/order/order',
  //  })
  //     })
  //   },
    //会员卡支付
    vipPay() {
      var userId = app.globalDataO.openid.objectSid
      var cantingid = this.data.cantingid
      var order = this.data.order
      var totalprice = this.data.totalprice
      var address = this.data.addressid
      var people = this.data.people
      var logfee = this.data.logfee
      var luck_id = this.data.id
      app.post(app.url.memberCardPay, {
        memberId: userId,
        restaurantId: cantingid,
        payOrderId: order,
        payOrderPrice: totalprice + logfee,
        seat: people,
        addressId: address,
        logfee: logfee,
        luck_id: luck_id
      }).then(res => {
        console.log(res)
        if (res.data.status == '000000') {
          wx.showToast({
            title: '会员卡支付成功',
          })
        } else {
          wx.showToast({
            title: res.data.message,
            image: '../../images/gantan.png'
          })
        }
      })
    }
  }
})